<?php

namespace app\miniprograms\controller;

use controller\BasicAdmin;
use service\DataService;
use service\NodeService;
use service\ToolsService;
use think\App;;
use think\Db;
use app\common\model\Enum;

/**
 * 机器管理
 * Class Machine
 * @package app\admin\controller
 * @author liuda <1576283429@qq.com>
 */
class Machine extends BasicAdmin
{
    public $table = 'machine';
    /**
     * 机器管理列表
     */
    public function index(){
        $this->title = '机器管理';
        $session = $this->request->session();
        $user = $session['user'];  //$user['is_admin']  1为管理员  其余是供应商
        $where = ' 1 ';
        $where.=" and A.is_deleted = 0 ";
        
        $area = $this->request->get('area');
        $code = $this->request->get('code');

        if(is_numeric($area)){
            $where .= " AND A.area_id = $area ";
        }

        if(!empty($code)){
            $where .= " AND A.machine_no = '$code' ";
        }
        
        if( $user['is_admin'] !=1 ){
           // $where.=" AND A.user_id={$user['id']} " ;
        }

       

        $areas = Db::table('area')->field('id,area_name')->where('is_deleted = 0')->select(); //获取地区
        $this->assign('areas', $areas);

        $this->assign('sme_type_array', Enum::sme_type_array);

        return parent::_list(Db::table('machine')->alias('A')->join('area E','A.area_id = E.id','LEFT')->join('machine_type mt','A.machine_type_id = mt.id','LEFT')->field('A.id, A.machine_no,A.imei,A.area_id, A.create_time,A.dormitory_num,E.area_name,mt.name,mt.brand,mt.supplier,A.status,A.surplus_time,A.last_heart,A.machine_type_id')->where($where)->order('A.id DESC'));
    }

    /**
     * 添加机器
     * @return array|string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\Exception
     */
    public function add()
    {
        $areas = Db::table('area')->field('id,area_name')->where('is_deleted = 0')->select(); //获取地区
        $this->assign('areas', $areas);

        $machine_type = Db::table('machine_type')->field('id,name,brand,supplier')->where('is_deleted = 0')->select(); //获取地区
        $this->assign('machine_type', $machine_type);


        $userWhere = [
            'status' => 1,
            'is_deleted' => 0,
            'is_admin' => 0
        ];
        $user = Db::name('SystemUser')->where($userWhere)->select();
        $this->assign('user', $user);
        return $this->_form($this->table, 'form');
    }


    /**
     * 表单数据默认处理
     * @param array $data
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function _form_filter($data)
    {
        if ($this->request->isPost()) {
            $isEdit = $this->request->post('edit');
            //编辑操作将以前的数据删除
            if($isEdit){
                $id =  $this->request->post('id');
                Db::name('machine')->where('id',$id)->delete();
            }
            $machineData = [];
            $machineData['machine_no'] = $this->request->post('machine_no');
            $machineData['imei'] = $this->request->post('imei');
            $machineData['area_id'] = $this->request->post('area');
            $machineData['machine_type_id'] = $this->request->post('machine_type_id');
            $machineData['create_time'] = time();
            $machineData['create_by_id'] = session('user.id');
            $machineData['is_deleted'] =0;
            $machineData['rfid'] =$this->request->post('rfid');
            $machineData['dormitory_num'] =$this->request->post('dormitory_num');
            $machineData['user_id'] =$this->request->post('user_id');
            try{
                Db::name('machine')->insert($machineData);
                $this->success('恭喜, 数据保存成功!', '');
            }catch (Exception $e){
                $this->error('添加数据失败，请稍后再试！');
            }
        } else {

        }
    }


   /*
    * 编辑机器
    */
    public function edit()
    {
        $id = $this->request->get('id');
        $machine = Db::table('machine')->where("id={$id}")->find();
        $this->assign('machine', $machine);
        $areas = Db::table('area')->field('id,area_name')->where('is_deleted = 0')->select(); //获取地区
        $machine_type = Db::table('machine_type')->field('id,name,brand,supplier')->where('is_deleted = 0')->select(); //获取地区
        $this->assign('machine_type', $machine_type);
        $this->assign('edit', true);
        $userWhere = [
            'status' => 1,
            'is_deleted' => 0,
            'is_admin' => 0
        ];
        $user = Db::name('SystemUser')->where($userWhere)->select();
        $this->assign('user', $user);
        $this->assign('areas', $areas);
        return $this->_form($this->table, 'form');
    }


    /**
     * 删除机器
     */
    public function del(){
        $id = $this->request->get('id');
        $isDel = Db::name('machine')->where('id',$id)->delete();
        if ($isDel) {
            $this->success("机器删除成功！", '');
        }else{
            $this->error("机器删除失败，请稍候再试！");
        }
    }

    public function log(){
        if ($this->request->isGet()) {
            $imei = $this->request->get('imei');
            $code = $this->request->get('code');

            $where = ' 1 ';
            $where.=" and A.imei = '$imei' ";

            $list = Db::table('machine_log')->alias('A')->field('A.status,A.surplus_time,A.cretime')->where($where)->order('A.id DESC')->limit(300)->select();
            // var_dump($list);
            $this->assign('imei', $imei);
            $this->assign('code', $code);
            $this->assign('sme_type_array', Enum::sme_type_array);
            $this->assign('list', $list);
           
            $this->title = '状态日志记录最近300条';
            return $this->_form($this->table, 'log');
        }


    }



}